package com.yami.shop.app.config;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.yami.shop.common.config.ApiContext;

import java.util.HashMap;
import java.util.Map;

public class PushMessageUtil {

    static PushApi pushApi;

    static ApiContext apiContext;

    public static void pushToSingleByCid(String cid,String message) throws InterruptedException {
        //初始数据
        apiContext = ApiContext.build();
        apiContext.configuration.setAnalyseStableDomainInterval(500);
        apiContext.configuration.setCheckHealthInterval(500);
        apiContext.configuration.setOpenAnalyseStableDomainSwitch(false);  //关闭
        ApiHelper apiHelper = ApiHelper.build(apiContext.configuration);
        pushApi = apiHelper.creatApi(PushApi.class);

        PushDTO<Audience> pushDTO = pushDTO();
        Audience audience = new Audience();
        audience.addCid(cid);
        pushDTO.setAudience(audience);
        while (true) {
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            PushMessage pushMessage = new PushMessage();
            pushMessage.setTransmission(message);
            pushDTO.setPushMessage(pushMessage);

            ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
            if (apiResult.isSuccess()) {
                System.out.println("成功"+apiResult.getData());
                // success
            } else {
                System.out.println("失败:code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
                // failed
            }
            System.err.println(apiResult);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            break;
        }
    }

    private static PushDTO<Audience> pushDTO() {
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("g-name1");

        Settings settings = new Settings();
        settings.setTtl(3600000);
        Strategy strategy = new Strategy();
        strategy.setSt(1);
        settings.setStrategy(strategy);

        pushDTO.setSettings(settings);

        PushMessage pushMessage = new PushMessage();
        GTNotification notification = new GTNotification();
        // notification.setBigImage("https://url");
        // notification.setLogo("https://url");
        notification.setLogoUrl("https://url");
        notification.setTitle("title-" + System.currentTimeMillis());
        notification.setBody("body1");
        notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_URL.type);
        notification.setUrl("https//:www.getui.com");

        pushMessage.setNotification(notification);
        pushDTO.setPushMessage(pushMessage);

        PushChannel pushChannel = new PushChannel();
        AndroidDTO androidDTO = new AndroidDTO();
        Ups ups = new Ups();
        ThirdNotification thirdNotification = new ThirdNotification();
        thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
        thirdNotification.setTitle("title-" + System.currentTimeMillis());
        thirdNotification.setBody("content");
        ups.setNotification(thirdNotification);


        //设置options 方式一
        ups.addOption("HW","badgeAddNum",3);
        ups.addOption("HW","badgeClass","com.getui.demo.GetuiSdkDemoActivity");
        ups.addOption("OP","app_message_id",11);
        ups.addOption("VV","message_sort",1);
        ups.addOptionAll("channel","default");


        //设置options 方式二
        Map<String, Map<String,Object>> options = new HashMap<String, Map<String, Object>>();
        Map<String,Object> all = new HashMap<String, Object>();
        all.put("channel","default");
        options.put("ALL",all);
        Map<String,Object> hw = new HashMap<String, Object>();
        all.put("badgeAddNum",3);
        all.put("badgeClass","com.getui.demo.GetuiSdkDemoActivity");
        options.put("HW",hw);
        ups.setOptions(options);


        androidDTO.setUps(ups);
        pushChannel.setAndroid(androidDTO);

        IosDTO iosDTO = new IosDTO();
        Aps aps = new Aps();
        Alert alert = new Alert();
        alert.setTitle("title-" + System.currentTimeMillis());
        alert.setBody("ios_body");
        aps.setAlert(alert);
        iosDTO.setAps(aps);
        pushChannel.setIos(iosDTO);
        pushDTO.setPushChannel(pushChannel);

        return pushDTO;
    }


}
